<?php
namespace App\Transformers;

use App\Models\Collect;
use App\Models\Comment;
use App\Models\Write;
use League\Fractal\TransformerAbstract;

class WriteTransformer extends TransformerAbstract
{
    protected $code;
    protected $message;


    protected $availableIncludes=['user','category'];
    public function transform(Write $write){
        return [
            'id'         =>$write->id,
            'user_id'      =>$write->user_id,
//            'name'       =>$write->name,
            'category_id'       =>$write->category_id,
            'cover'      =>$write->cover,
            'title'      =>$write->title,
            'subs'       =>$write->subs,
            'view_num'   =>$write->view_num,
            'is_on'      =>$write->is_on,
            'created_at' =>$write->created_at,
            'sub_num' =>Comment::where('write_id',$write->id)
                ->count(),
            'is_collected'=>Collect::where('user_id',$write->user_id)->where('write_id',$write->id)->select(['is_locked'])->where('is_locked',0)->get(),
//            'updated_at' =>$user->updated_at
        ];
    }
    public function IncludeUser(Write $write){
        return $this->item($write->user,new UserTransformer());
    }
    public function IncludeCategory(Write $write){
        return $this->item($write->category,new CategoryTransformer());
    }
}
